<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <el-table v-loading="loading" :data="bigGoodsList" @row-click="handleRowClick" @selection-change="handleSelectionChange">
          <el-table-column label="大类名称" align="center" prop="goodsName" />
        </el-table>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="类别名称" prop="goodsName">
            <el-input
              v-model="queryParams.goodsName"
              placeholder="请输入类别名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="存储方式" prop="storageTypeId">
            <el-select v-model="queryParams.storageTypeId" placeholder="请选择所属冷库">
              <el-option
                v-for="cold in TypeList"
                :key="cold.coldTypeId"
                :label="cold.typeName"
                :value="cold.coldTypeId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input
              v-model="queryParams.description"
              placeholder="请输入描述"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="质检方案" prop="qualityPlan">
            <el-input
              v-model="queryParams.qualityPlan"
              placeholder="请输入质检方案"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建人" prop="createBy">
            <el-input
              v-model="queryParams.createBy"
              placeholder="请输入创建人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddBig"
              v-hasPermi="['storage:goods:add']"
            >新增大类</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteBig"
              v-hasPermi="['storage:goods:delete']"
            >删除大类</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['storage:goods:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['storage:goods:update']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['storage:goods:delete']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['storage:goods:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="smallGoodsList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" width="80">
            <template slot-scope="scope">
              {{ (scope.$index + 1) + (queryParams.pageNum - 1) * queryParams.pageSize }}
            </template>
          </el-table-column>
          <el-table-column label="所属大类" align="center" prop="parentName" />
          <el-table-column label="类别名称" align="center" prop="goodsName" />
          <el-table-column label="建议存储" align="center" prop="typeName" />
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="质检方案" align="center" prop="qualityPlan" />
<!--          <el-table-column label="创建人" align="center" prop="createBy" />-->
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['storage:goods:update']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['storage:goods:delete']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <div style="float: left;margin-left: 10px;padding-bottom:50px;width: 1000px">
      <!-- 添加或修改岗位对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="所属大类" prop="coldId">
            <el-select v-model="form.parentId" placeholder="请选择所属冷库">
              <el-option
                v-for="bigGood in bigGoodsList"
                :key="bigGood.goodsId"
                :label="bigGood.goodsName"
                :value="bigGood.goodsId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类别名称" prop="goodsName">
            <el-input v-model="form.goodsName" placeholder="请输入类别名称" />
          </el-form-item>
          <el-form-item label="建议存储" prop="storageTypeId">
            <el-select v-model="form.storageTypeId" placeholder="请选择建议存储">
              <el-option
                v-for="type in TypeList"
                :key="type.coldTypeId"
                :label="type.typeName"
                :value="type.coldTypeId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入描述" />
          </el-form-item>
          <el-form-item label="质检方案" prop="qualityPlan">
            <el-input v-model="form.qualityPlan" placeholder="请输入质检方案" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 添加大类对话框 -->
      <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="大类名称" prop="goodsName">
            <el-input v-model="form.goodsName" placeholder="请输入大类名称" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm2">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 删除大类对话框 -->
      <el-dialog :title="title3" :visible.sync="open3" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="删除大类" prop="goodsId">
            <el-select v-model="form.goodsId" placeholder="请选择要删除的大类">
              <el-option
                v-for="type in bigGoodsList"
                :key="type.goodsId"
                :label="type.goodsName"
                :value="type.goodsId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm3">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import {
  getParent,
  getChild,
  getAllType,
  addGoods,
  addGoods2,
  getGoodsById,
  updateGoods,
  getTotal,
  listPost,
  getPost,
  delPost,
  addPost,
  updatePost,
  deleteGoods,
  delGoods
} from "@/api/storage/goods";
import store from "@/store";
import {deleteBigCold} from "@/api/storage/bigCold";
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      userId:store.state.user.id, //将缓存的用户id赋值给userId
      userName:store.state.user.name, //将缓存的用户id赋值给userId
      //存储大类
      bigGoodsList: [],
      //存储小类
      smallGoodsList: [],
      //存储现在的大类id
      nowBigGoodsId: '',
      //存储存储类别
      TypeList: [],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      title2: "新增大类",
      title3: "删除大类",
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        parentId: undefined,
        storageTypeId: undefined,
        description: undefined,
        qualityPlan: undefined,
        createBy: undefined,
        beginTime: undefined,
        endTime: undefined
      },
      queryParams2:{
        pageNum: 1,
        pageSize: 999,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getParentSome();
    this.getAllTypeSome();
    // this.getChildSome();
  },
  methods: {
    //获得存储类型
    getAllTypeSome() {
      getAllType(this.queryParams2).then(response => {
        this.TypeList = response.data;
      });
    },
    //获得大类
    getParentSome() {
      getParent().then(response => {
        this.bigGoodsList = response.data;
        console.log(this.bigGoodsList)
        this.getChildSome();
      });
    },
    //弹出大类id
    handleRowClick(row){
      this.nowBigGoodsId = row.goodsId;
      this.queryParams.parentId = this.nowBigGoodsId;
      this.getChildSome();
    },
    //获得所有小类
    getChildSome() {
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      getChild(this.queryParams).then(response => {
        this.smallGoodsList = response.data;
        console.log(this.smallGoodsList);
        for (var i = 0; i < this.smallGoodsList.length; i++){
          for (var j = 0; j < this.bigGoodsList.length; j++){
            if(this.smallGoodsList[i].parentId === this.bigGoodsList[j].goodsId){
              this.smallGoodsList[i].parentName = this.bigGoodsList[j].goodsName;
            }
          }
        }
        this.loading = false;
      })
      this.getCountSome();
    },
    //获得条数
    getCountSome() {
      getTotal(this.queryParams).then(response => {
        this.total = response.data;
      })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.getParentSome();
      this.getCountSome();
      // listPost(this.queryParams).then(response => {
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.open3 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getChildSome();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.form = {};
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增大类操作 */
    handleAddBig(){
      this.reset();
      this.open2 = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货物类别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids
      getGoodsById(goodsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货物类别";
      });
    },
    /** 提交大类 */
    submitForm2: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != undefined) {
            return;
          }else {
            this.form.createBy = this.userName;
            addGoods2(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open2 = false;
              this.getParentSome();
              this.getChildSome();
            });
          }
        }
      });
    },
    /** 删除大类 */
    submitForm3: function() {
      const goodsId = this.form.goodsId;
      this.$modal.confirm('是否确认删除大类数据项？').then(() => {
        delGoods(goodsId).then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList(); // 此处的 this 指向 Vue 实例
        });
        this.reset();
      }).catch(() => {});
      this.open3 = false;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != undefined) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getParentSome();
              this.getChildSome();
            });
          } else {
            this.form.createBy = this.userName;
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getParentSome();
              this.getChildSome();
            });
          }
        }
      });
    },
    /** 删除大类 */
    handleDeleteBig(){
      this.open3 = true;
      this.form = {};
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal.confirm('是否确认删除冷库数据项？').then(function() {
        return deleteGoods(goodsIds);
      }).then(() => {
        this.getChildSome();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
